
use "hierarchy_replication.dta"
xtset ccode year 


**Controls
global cntrl wi_compete  wi_lnpop_wdi wi_lngdppc wi_polity2 wi_polity2_2  wi_ny_gdp_totl_rt_zs wi_cwyrs wi_c2 wi_c3 coldwar 



// Table 1: 


// 
est clear
*model 1
reg F.wi_v2stfisccap2   wi_usa_median $cntrl  , robust
 estimates store m1
 cap drop sample
 gen sample =1 if e(sample)
 *model 2
reg wi_v2xcl_prpty   wi_usa_median $cntrl  if sample==1 , robust
 estimates store m2
  *model 3
reg F.wi_v2stfisccap2 wi_v2xcl_prpty  wi_usa_median $cntrl if sample==1 , robust
 estimates store m3




 
// estimation of the mediation effect.  
// Added these to the table by hand
paramed wi_Fv2stfisccap2 , avar(wi_usa_median) mvar(wi_v2xcl_prpty ) ///
cvars($cntrl)  a0(0)  a1(1) m(0) ///
yreg(linear) mreg(linear) nointeraction 
 estimates store med1

 *model 4 (IV)
ivreg2 Fwi_v2stfisccap2 wi_v2xcl_prpty $cntrl ///
 (wi_usa_median=Echelon2),  gmm2s  first 
  estimates store iv1
weakivtest
 
 *model 5
ivreg2 Fwi_v2xcl_prpty  $cntrl ///
 (wi_usa_median=Echelon2) if sample==1, gmm2s first 
  estimates store iv2 
 weakivtest

 
  **Output
estout m1 m2 m3 iv2 iv1 , cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats( r2 N, fmt(%9.2f %9.0f) labels("R-Sq" ))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(wi_usa_median wi_v2xcl_prpty ) drop(wi_c2 wi_c3) ///
mlabels ("Model 1" "Model 2" "Model 3" "Model 4" "Model 5" ) ///
title("") 


// Add indirect effect for IV models and iv diagnostics by hand  
 
 
 gmm                                      ///
    (eq1: Fwi_v2xcl_prpty -{b1}*wi_usa_median -{b2}*wi_compete -{b3}* wi_lnpop_wdi  ///
	-{b4}*wi_lngdppc -{b5}*wi_polity2 -{b6}*wi_polity2_2 -{b7}*wi_ny_gdp_totl_rt_zs -{b8}*wi_cwyrs - ///
	{b9}*wi_c2 -{b10}*wi_c3 - {b11}*coldwar -{b0} ) ///
    (eq2: Fwi_v2stfisccap2 -{c1}*wi_usa_median -{c2}*wi_compete -{c3}*wi_lnpop_wdi  ///
	-{c4}*wi_lngdppc -{c5}*wi_polity2-{c6}*wi_polity2_2 -{c7}*wi_ny_gdp_totl_rt_zs -{c8}*wi_cwyrs - ///
	{c9}*wi_c2 -{c10}*wi_c3 - {c11}*coldwar - {c12}*wi_v2xcl_prpty  -{c0} )  ,      ///
    instruments(eq1: Echelon2 $cntrl )     ///
    instruments(eq2: Echelon2 wi_v2xcl_prpty $cntrl )             ///
	twostep  winitial(unadjusted, indep)  wmatrix(cluster ccode)


*here's the indirect test:
      nlcom  ([b1]_cons *[c12]_cons) 
 
 
 *Alt. indirect effect (assuming the PR is endogenous; not shown)
 ivmediate Fwi_v2stfisccap2   wi_compete wi_lnpop_wdi wi_lngdppc wi_polity2 wi_polity2_2  wi_ny_gdp_totl_rt_zs wi_cwyrs wi_c2 wi_c3 coldwar ///
, mediator( wi_v2xcl_prpty ) treatment(wi_usa_median) instrument(Echelon2)
  
 
// Figure 3 A

estimates restore m1
 
margins , at( wi_usa_median =(-2(.1)2)) saving(file1, replace)
marginsplot, recast(line) recastci(rarea) yline(0) title("Unmediated") ytitle("State Capacity") graphregion(color(white)) name(g1, replace)

est restore m3 
 margins , at( wi_usa_median =(-2(.1)2)) saving(file2, replace)

combomarginsplot file1 file2, labels("Unmediated" "Mediated") recast(line) recastci(rarea)   graphregion(color(white)) ///
  file1opts(lpattern("--") lcolor(black)  ) file2opts(lpattern(line) lcolor(red)) ciopts(color(%10)) ytitle("State Capacity") title("")

   
// Figure 3 B

est restore m3 
margins , at( wi_v2xcl_prpty=(-.1(.05).1) wi_usa_median=0) post
	est store try1
est restore m3 
margins , at( wi_v2xcl_prpty=(-.1(.05).1) wi_usa_median=1) post
	est store try2

coefplot try1 try2, at xtitle("Property Rights") ytitle("State Capacity") recast(line) noci lwidth(*1) connect(l) xline(0 0.0273, lpattern(dot) lcolor(black)) ///
 yline(-0.012 0.0613, lpattern(dot) lcolor(black)) graphregion(color(white))  ylabel(,nogrid) ///
 legend(label(1 "Mean U.S. Support") label(2 "Mean U.S. Support + 1"))
// add annotations by hand. 	
	
	
// Figure 3 C
// -- see R code mediate_replication_code_R.R



// Table 2 

*Controls
global cwcntrl wi_lnpop_wdi btw_Fmax_civilwar1 wi_lngdppc wi_compete wi_polity2 wi_polity2_2 wi_ny_gdp_totl_rt_zs wi_cwyrs wi_c2 wi_c3 coldwar



 regress wi_v2stfisccap2  wi_usa_median  $cwcntrl , robust
 estimates store b1

*model 6
 logit Fmax_civilwar1  wi_usa_median wi_v2stfisccap2 $cwcntrl , robust
 estimates store b2
 
 *Estimating indirect effects (without property rights)
  paramed Fmax_civilwar1, avar(wi_usa_median) mvar(wi_v2stfisccap2) ///
 cvars($cwcntrl)  a0(0)  a1(1) m(0) ///
yreg(logistic) mreg(linear) nointeraction  
  estimates store med4 
 
 
 *model 6 replication (linear probability model)
 regress wi_v2xcl_prpt  wi_usa_median wi_v2stfisccap2  $cwcntrl , robust
  estimates store b3

 
 **Model 7
 logit Fmax_civilwar1  wi_usa_median  wi_v2xcl_prpt wi_v2stfisccap2 $cwcntrl , robust
 estimates store b4
 
 paramed Fmax_civilwar1, avar(wi_usa_median) mvar(wi_v2xcl_prpt) ///
 cvars($cwcntrl )  a0(0)  a1(1) m(0) ///
yreg(logistic) mreg(linear) nointeraction  
  estimates store med5
 


 
 
 **Model 8
  
 ivprobit Fmax_civilwar1 wi_v2xcl_prpt wi_v2stfisccap2 $cwcntrl  ///
 (wi_usa_median =Echelon2), first vce(cluster ccode) 
  estimates store iv3p
 
  


*Output Table 2
 
       estout  b2  b4 iv3p, cells(b(star fmt(%9.3f)) se(par fmt(%9.3f))) starlevels(* 0.05 ) ///
stats(  aic N, fmt( %9.1f %9.0f) labels("AIC"))  style(tex) ///
legend label collabels(none) varlabels(_cons Constant) ///
order(wi_usa_median  wi_v2xcl_prpty ) drop(wi_c2 wi_c3 ) ///
mlabels ("Model 1" "Model 2" "Model 3"  ) ///
title("")
 

 **Indirect effects (Bottom of Table 2)
reg F.wi_v2stfisccap2  wi_usa_median $cntrl if sample==1 
 estimates store m1
 reg wi_v2xcl_prpty   wi_usa_median $cntrl  if sample==1 
 estimates store m2
reg F.wi_v2stfisccap2 wi_v2xcl_prpty  wi_usa_median $cntrl if sample==1 
 estimates store m3
 logit Fmax_civilwar1  wi_usa_median   wi_v2stfisccap2 $cwcntrl
 estimates store b3
logit Fmax_civilwar1  wi_usa_median  wi_v2xcl_prpt wi_v2stfisccap2 $cwcntrl
 estimates store b4
 
  suest m1 m2 m3 b3 b4 
 
 *Indirect effect of only capacity as mediator
 nlcom [m1_mean]_b[wi_usa_median]*[b3_Fmax_civilwar1]_b[wi_v2stfisccap2]

  *Indirect effect of capacity and PR as mediators

nlcom [m2_mean]_b[wi_usa_median]*[b4_Fmax_civilwar1]_b[wi_v2xcl_prpty] + [m3_mean]_b[wi_usa_median]*[b4_Fmax_civilwar1]_b[wi_v2stfisccap2]

**indirect effects with IV
  gmm                                      ///
    (eq1: Fwi_v2xcl_prpty -{b1}*wi_usa_median -{b2}*wi_compete -{b3}* wi_lnpop_wdi  ///
	-{b4}*wi_lngdppc -{b5}*wi_polity2 -{b6}*wi_polity2_2 -{b7}*wi_ny_gdp_totl_rt_zs -{b8}*wi_cwyrs - ///
	{b9}*wi_c2 -{b10}*wi_c3 - {b11}*coldwar -{b0} ) ///
    (eq2: Fwi_v2stfisccap2 -{c1}*wi_usa_median -{c2}*wi_compete -{c3}*wi_lnpop_wdi  ///
	-{c4}*wi_lngdppc -{c5}*wi_polity2-{c6}*wi_polity2_2 -{c7}*wi_ny_gdp_totl_rt_zs -{c8}*wi_cwyrs - ///
	{c9}*wi_c2 -{c10}*wi_c3 - {c11}*coldwar - {c12}*wi_v2xcl_prpty  -{c0} ) ///
	  (eq3: Fmax_civilwar1 - (invlogit({xb: wi_usa_median btw_Fmax_civilwar1 wi_v2stfisccap2 wi_v2xcl_prpty $cntrl} + {d0}))) ,      ///
    instruments(eq1: Echelon2 $cntrl )     ///
    instruments(eq2: Echelon2 wi_v2xcl_prpty $cntrl )             ///
	    instruments(eq3: Echelon2 btw_Fmax_civilwar1 wi_v2stfisccap2 wi_v2xcl_prpty $cntrl )             ///
		twostep  winitial(unadjusted, indep)  wmatrix(cluster ccode)

		
      nlcom  ([b1]_cons * [xb]wi_v2xcl_prpty) + ([c1]_cons *[xb]wi_v2stfisccap2)
	
	


	
	
	
